package com.example.course.controller;

import com.example.course.bean.Course;
import com.example.course.bean.Student;
import com.example.course.bean.Teacher;
import com.example.course.bean.User;
import com.example.course.service.CourseService;
import com.example.course.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CourseController {
    @Autowired
    private CourseService courseService;
    @ResponseBody
    @RequestMapping("/course/showCourse")
    public Result showTeachers() {
        return new Result(courseService.getCourse());
    }

    @ResponseBody
    @RequestMapping("/course/updateCourse")
    public Result updateCourse(Course course) {
        int i = courseService.updateCourse(course);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }

    @ResponseBody
    @RequestMapping("/course/delCourse/{id}")
    public Result deleteCourse(@PathVariable int id) {

        int i = courseService.deleteCourse(id);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }
    @ResponseBody
    @RequestMapping("/course/insCourse")
    public Result insCourse(Course course, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Teacher teacher = (Teacher) user.getData();
        int i = courseService.insertCourse(course,teacher.getId());
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }
    @ResponseBody
    @RequestMapping("/course/findTeacherCourse")
    public Result insCourse(HttpSession session) {
        User user = (User)session.getAttribute("user");
        Teacher teacher = (Teacher) user.getData();
        List<Course> courseList = courseService.getCourseList(teacher.getId());
        return new Result(courseList);

    }
    @ResponseBody
    @RequestMapping("/course/findStudentCourse")
    public Result findStudentCourse(HttpSession session) {
        User user = (User)session.getAttribute("user");
        Student student = (Student) user.getData();
        List<Course> courseList = courseService.getCourseByStudentId(student.getId());
        return new Result(courseList);

    }

}
